package com.thoughtworks.turtlemock.expression;

public class Measurement implements Expression {
    public static final String CENTIMETER = "centimeter";
    public static final String MILLIMETER = "millimeter";

    public static Measurement centimeter(int value) {
        return new Measurement(value, CENTIMETER);
    }

    public static Measurement millimeter(int value) {
        return new Measurement(value, MILLIMETER);
    }

    final double value;
    private final String unit;

    public Measurement(double value, String unit) {
        this.value = value;
        this.unit = unit;
    }

    public Expression plus(Expression expression) {
        return new Sum(this, expression);
    }

    public Expression convert(String to) {
        return new Measurement(value * Conversion.rate(unit, to), to);
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        final Measurement that = (Measurement) o;

        if (value != that.value) return false;
        if (unit != null ? !unit.equals(that.unit) : that.unit != null) return false;

        return true;
    }

    public int hashCode() {
        int result;
        long temp;
        temp = value != +0.0d ? Double.doubleToLongBits(value) : 0L;
        result = (int) (temp ^ (temp >>> 32));
        result = 29 * result + (unit != null ? unit.hashCode() : 0);
        return result;
    }

    public String toString() {
        return "unit: " + unit + "; value:" + value;
    }
}
